/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.mdd.siegfried;

import java.io.*;

import java.util.LinkedList;

import java.util.zip.*;

public class Zip {

    public static void run(LinkedList<String> filenames) {


// Create a buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // Create the ZIP file
            String outFilename = "outfile.zip";
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

            // Compress the files
            for (int i = 0; i < filenames.size(); i++) {
                FileInputStream in = new FileInputStream(filenames.get(i));

                // Add ZIP entry to output stream.
                //
                System.out.println(filenames.get(i));
                File f = new File(filenames.get(i));
                
                String dirFix = filenames.get(i).substring(0, filenames.get(i).lastIndexOf("/"));
                File dir = new File(dirFix);
                System.out.println(dirFix);
                String relativePath =  dir.toURI().relativize(f.toURI()).getPath();
                System.out.println(relativePath);
                out.putNextEntry(new ZipEntry(relativePath));
                

                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            }

            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
        }
    }
}